package com.simon.gpsinfo;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GpsInfoActivity extends Activity {
	private TextView Accuracy;
	private TextView Altitude;
	private TextView Bearing;

	private TextView Latitude;
	private TextView Longitude;
	private TextView Provider;
	private TextView Speed;
	private TextView Speed1;
	private TextView Time;

	private LocationManager locationManager;
	private LocationListener locationListener;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((Button) findViewById(R.id.startmeasuring))
				.setOnClickListener(onButtonClicked);
		((Button) findViewById(R.id.stopmeasuring))
				.setOnClickListener(onButtonClicked);

		enableButtons(false);

	}

	public void startMeasuring() {
		try {
			// Acquire a reference to the system Location Manager
			locationManager = (LocationManager) this
					.getSystemService(Context.LOCATION_SERVICE);

			// Define a listener that responds to location updates
			locationListener = new LocationListener() {
				public void onLocationChanged(Location location) {

					makeUseOfNewLocation(location);
				}

				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				}

				public void onProviderEnabled(String provider) {
				}

				public void onProviderDisabled(String provider) {
				}
			};
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, 0, 0, locationListener);
		} catch (final Exception ex) {

		}
		enableButtons(true);
	}

	public void stopMeasuring() {
		try {
			if (locationManager != null && locationListener != null) {
				locationManager.removeUpdates(locationListener);
			}

			locationManager = null;
		} catch (final Exception ex) {

		}
		enableButtons(false);
	}

	private void enableButtons(boolean isMeasuring) {
		((Button) findViewById(R.id.startmeasuring))
				.setEnabled(isMeasuring ? false : true);
		((Button) findViewById(R.id.stopmeasuring))
				.setEnabled(isMeasuring ? true : false);
	}

	private View.OnClickListener onButtonClicked = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.startmeasuring: {
				startMeasuring();

				break;
			}
			case R.id.stopmeasuring: {
				stopMeasuring();

				break;
			}
			}
		}
	};

	// TO DO - przenies calosc do klasy manager
	private void makeUseOfNewLocation(Location location) {
		// DBGPSInfo dbgpsinfo = new DBGPSInfo(this);
		Accuracy = (TextView) findViewById(R.id.Accuracy);
		Altitude = (TextView) findViewById(R.id.Altitude);
		Bearing = (TextView) findViewById(R.id.Bearing);
		Speed = (TextView) findViewById(R.id.Speed);
		Speed1 = (TextView) findViewById(R.id.Speed1);

		Latitude = (TextView) findViewById(R.id.Latitude);
		Longitude = (TextView) findViewById(R.id.Longitude);
		Provider = (TextView) findViewById(R.id.Provider);
		Time = (TextView) findViewById(R.id.Time);

		float accuracy = location.getAccuracy();
		double altitude = location.getAltitude();
		float bearing = location.getBearing();
		float speed = location.getSpeed();

		double latitude = location.getLatitude();
		double longitude = location.getLongitude();
		String provider = location.getProvider();

		DecimalFormat decimalFormat = new DecimalFormat("####.######");

		if (location.hasAccuracy()) {

			Accuracy.setText("" + decimalFormat.format(accuracy) + " [m]");

		} else {
			Accuracy.setText("brak danych");
		}

		if (location.hasAltitude()) {
			Altitude.setText("" + decimalFormat.format(altitude) + " [m]");
		} else {
			Altitude.setText("brak danych.");
		}

		if (location.hasBearing()) {
			Bearing.setText("" + decimalFormat.format(bearing));
		} else {
			Bearing.setText("Namiar off.");
		}
		if (location.hasSpeed()) {
			Speed.setText("" + decimalFormat.format(speed) + " [m/s]");
			Speed1.setText("" + decimalFormat.format((speed * 3.6)) + " [km/h]");
		} else {
			Speed.setText("brak danych.");
			Speed1.setText("brak danych.");
		}

		Latitude.setText("" + decimalFormat.format(latitude));
		Longitude.setText("" + decimalFormat.format(longitude));
		Provider.setText("" + provider);

		try {
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(location.getTime());
			String format = "dd/MM/yyyy HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			String dateString = sdf.format(cal.getTime());
			Time.setText("" + dateString);
		} catch (NullPointerException exp) {
			exp.getMessage();
		} catch (IllegalArgumentException exp) {
			exp.getMessage();
		}

		// dbgpsinfo.createRow(location.getTime(), speed, accuracy, altitude,
		// bearing, latitude, longitude, provider);

	}
}
